<template>
  <div class="container">
    <div class="login">
      <h1>{{ title }}</h1>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="account">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="请输入内容"
            v-model="form.account"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')"
            >立即登录</el-button
          >
          <el-button type="primary" @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "../../../utils/api";
import { setToken } from "../../../utils/token";
export default {
  name: "login",
  data() {
    return {
      title: "信贷管理系统",
      form: {
        account: "admin",
        password: "admin@123",
      },
      rules: {
        account: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // this.$http
          //   .post("user/login", this.form)
          //   .then((res) => {
          //     console.log(res);
          //     if (res.data.code == 20000) {
          //       this.$router.push("/index");
          //     }
          //   })
          //   .catch((err) => {
          //     console.log(err);
          //   });
          login(this.form).then((res) => {
            // console.log(res);
            let { token, account } = res.data.data;
            if (res.data.code == 20000) {
              this.$router.push("/index");
              setToken(token);
              sessionStorage.setItem("accountKey", account);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.form.account = this.form.password = "";
    },
  },
};
</script>

<style lang="scss"  scoped>
.login {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -150px;
  margin-left: -200px;
  text-align: center;
  width: 450px;
  padding: 20px 40px 0 0;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  h1 {
    margin-left: 40px;
  }
  .el-form-item:last-child {
    float: right;
  }
}
</style>